﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Admin;
using BusinessObjects.BusinessRules;
using DataObjects.AdoNet;
using System.Data;
using BusinessObjects;

namespace DataObjects.SqlServer.Admin
{
    public class SqlServerAutoIdDao : IAutoIdDao
    {
        public List<AutoID> GetAutoIds() { return null; }

        public AutoID GetAutoIDByBranch(string id, string restaurantId)
        {
            string procedures = "Get_AutoIDs_ByCondition";

            object[] parms = { "@WhereClause", " WHERE AutoID = '" + id + "' AND " + 
                                                       " RestaurantID = '" + restaurantId + "'" };
            return Db.Read(procedures, true, Make, parms);
        }

        public AutoID GetAutoIDRefTypeByBranch(int refTypeId, string restaurantId)
        {
            string procedures = "Get_AutoIDs_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RefTypeID = " + refTypeId + " AND " + 
                                                       " RestaurantID = '" + restaurantId + "'" };
            return Db.Read(procedures, true, Make, parms);
        }

        public void InsertAutoId() { }

        public void UpdateAutoId(AutoID autoId)
        {
            string sql = "Update_AutoID";

            Db.Update(sql, true, Take(autoId));
        }

        public void DeleteAutoId() { }

        private object[] Take(AutoID autoId)
        {
            return new object[]  
            {
                "@AutoID", autoId.AutoId,
                "@RefTypeID", autoId.RefTypeID,
                "@RefTypeName", autoId.RefTypeName,
                "@Prefix", autoId.Prefix,
                "@Value", autoId.Value,
                "@LengthOfValue", autoId.LengthOfValue,
                "@Suffix", autoId.Suffix,
                "@RestaurantID", autoId.RestaurantID
            };
        }

        private static Func<IDataReader, AutoID> Make = reader =>
            new AutoID
            {
                AutoId = reader["AutoID"].AsString(),
                RefTypeID = reader["RefTypeID"].AsInt(),
                RefTypeName = reader["RefTypeName"].AsString(),
                Prefix = reader["Prefix"].AsString(),
                Value = reader["Value"].AsInt(),
                LengthOfValue = reader["LengthOfValue"].AsInt(),
                Suffix = reader["Suffix"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString()
            };
    }
}